package love.bot.forli.group.review.config;

import lombok.RequiredArgsConstructor;
import love.bot.forli.group.review.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 无以铭川
 */
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

    private final AuthenticationInterceptor authenticationInterceptor;

    /**
     * 不拦截的URL集合
     */
    private static final List<String> EXCLUSION_URL_LIST = new ArrayList<>();

    static {
        EXCLUSION_URL_LIST.add("/login");
        EXCLUSION_URL_LIST.add("/doc.html");
        EXCLUSION_URL_LIST.add("/swagger-resources");
        EXCLUSION_URL_LIST.add("/test/*");
        EXCLUSION_URL_LIST.add("/test/create");
        EXCLUSION_URL_LIST.add("/test/submit");
        EXCLUSION_URL_LIST.add("/webjars/**");
        EXCLUSION_URL_LIST.add("/code/get");
        EXCLUSION_URL_LIST.add("/code/use");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**").excludePathPatterns(EXCLUSION_URL_LIST);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}
